package com.xinchao.service.impl;

import com.xinchao.constant.DateConstant;
import com.xinchao.constant.RequestParameterConstant;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.dto.PlayDurationWatchSumDTO;
import com.xinchao.model.dto.TouchWatchSumDTO;
import com.xinchao.model.vo.AvgWatchSumVO;
import com.xinchao.model.vo.ExposuresNumberVO;
import com.xinchao.model.vo.TouchWatchSumVO;
import com.xinchao.service.AdvertisementMonitorBaseDataSerivce;
import com.xinchao.service.AdvertisementMonitorService;
import com.xinchao.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 广告监播-基础数据
 * @author dxy
 * @date 2018/11/12 11:16
 */
@Service(value = "advertisementMonitorBaseDataSerivce")
public class AdvertisementMonitorBaseDataSerivceImpl implements AdvertisementMonitorBaseDataSerivce {
	@Autowired
	private AdvertisementMonitorService advertisementMonitorService;

	/**
	 * 获取广告曝光次数列表
	 * @param advertisementName 广告名称
	 * @param timeType          时间类型
	 * @return List<ExposuresNumberVO>
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@Override
	public List<ExposuresNumberVO> listExposuresNumberVO(String advertisementName, Integer timeType) throws ServiceException, ParseException {
		if (StringUtils.isBlank(advertisementName) || timeType == null) {
			throw new ServiceException("输入参数不能为空");
		}
		//开始时间
		Long startTime = null;
		//结束时间
		Long endTime = null;
		//时间列表
		List<String> dayList = new ArrayList<>();
		//近7天
		if (RequestParameterConstant.TIME_TYPE_NEAR_SEVEN_DAY == timeType) {
			String[] recentDay = DateUtils.getRecentDay(7, DateConstant.DATE_SHORT_ISO);
			dayList = Arrays.asList(recentDay);
		}
		//近30天
		if (RequestParameterConstant.TIME_TYPE_NEAR_THIRTY_DAY == timeType) {
			String[] recentDay = DateUtils.getRecentDay(30, DateConstant.DATE_SHORT_ISO);
			dayList = Arrays.asList(recentDay);
		}
		//返回报表列表
		List<ExposuresNumberVO> voList = new ArrayList<>();
		//获取数据
		for (String day : dayList) {
			startTime = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MIN, DateConstant.LONG_DATE_FORMAT_STR).longValue();
			endTime = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MAX, DateConstant.LONG_DATE_FORMAT_STR).longValue();
			Long exposuresSum = advertisementMonitorService.sumExposures(advertisementName, startTime, endTime);
			if (exposuresSum == null) {
				continue;
			}
			ExposuresNumberVO vo = new ExposuresNumberVO();
			vo.setTime(day);
			vo.setExposuresSum(exposuresSum);
			voList.add(vo);
		}
		return voList;
	}

	/**
	 * 通过广告名称统计总时长
	 * @param advertisementName 广告名称
	 * @param timeType          时间类型
	 * @return List<TouchWatchSumVO>
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@Override
	public List<TouchWatchSumVO> listTouchWatchSumVO(String advertisementName, Integer timeType) throws ServiceException, ParseException {
		if (StringUtils.isBlank(advertisementName) || timeType == null) {
			throw new ServiceException("输入参数不能为空");
		}
		//开始时间
		Long startTime = null;
		//结束时间
		Long endTime = null;
		//时间列表
		List<String> dayList = new ArrayList<>();
		//近7天
		if (RequestParameterConstant.TIME_TYPE_NEAR_SEVEN_DAY == timeType) {
			String[] recentDay = DateUtils.getRecentDay(7, DateConstant.DATE_SHORT_ISO);
			dayList = Arrays.asList(recentDay);
		}
		//近30天
		if (RequestParameterConstant.TIME_TYPE_NEAR_THIRTY_DAY == timeType) {
			String[] recentDay = DateUtils.getRecentDay(30, DateConstant.DATE_SHORT_ISO);
			dayList = Arrays.asList(recentDay);
		}
		//返回报表列表
		List<TouchWatchSumVO> voList = new ArrayList<>();
		//获取数据
		for (String day : dayList) {
			startTime = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MIN, DateConstant.LONG_DATE_FORMAT_STR).longValue();
			endTime = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MAX, DateConstant.LONG_DATE_FORMAT_STR).longValue();
			TouchWatchSumDTO dto = advertisementMonitorService.getTouchWatchSumDTO(advertisementName, startTime, endTime);
			if (dto == null) {
				continue;
			}
			TouchWatchSumVO vo = new TouchWatchSumVO();
			vo.setTime(day);
			vo.setTouchSum(dto.getTouchSum());
			vo.setWatchSum(dto.getWatchSum());
			voList.add(vo);
		}
		return voList;
	}

	/**
	 * 获取广告曝光次数列表
	 * @param advertisementName 广告名称
	 * @param timeType          时间类型
	 * @return List<AvgWatchSumVO>
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@Override
	public List<AvgWatchSumVO> listAvgWatchSumVO(String advertisementName, Integer timeType) throws ServiceException, ParseException {
		if (StringUtils.isBlank(advertisementName) || timeType == null) {
			throw new ServiceException("输入参数不能为空");
		}
		//开始时间
		Long startTime = null;
		//结束时间
		Long endTime = null;
		//时间列表
		List<String> dayList = new ArrayList<>();
		//近7天
		if (RequestParameterConstant.TIME_TYPE_NEAR_SEVEN_DAY == timeType) {
			String[] recentDay = DateUtils.getRecentDay(7, DateConstant.DATE_SHORT_ISO);
			dayList = Arrays.asList(recentDay);
		}
		//近30天
		if (RequestParameterConstant.TIME_TYPE_NEAR_THIRTY_DAY == timeType) {
			String[] recentDay = DateUtils.getRecentDay(30, DateConstant.DATE_SHORT_ISO);
			dayList = Arrays.asList(recentDay);
		}
		//返回报表列表
		List<AvgWatchSumVO> voList = new ArrayList<>();
		//获取数据
		for (String day : dayList) {
			startTime = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MIN, DateConstant.LONG_DATE_FORMAT_STR).longValue();
			endTime = DateUtils.stringTimeToLongTimeStamp(day + DateConstant.H_M_S_MAX, DateConstant.LONG_DATE_FORMAT_STR).longValue();
			PlayDurationWatchSumDTO dto = advertisementMonitorService.getPlayDurationWatchDTO(advertisementName, startTime, endTime);
			if (dto == null) {
				continue;
			}
			AvgWatchSumVO vo = new AvgWatchSumVO();
			vo.setTime(day);
			//观看人次
			Long watchSum = dto.getWatchSum();
			//总时长
			Long playDurationSum = dto.getPlayDurationSum();
			if (watchSum == null || watchSum == 0) {
				vo.setAvgWatchSum(0D);
			} else {
				if (playDurationSum == null || playDurationSum == 0) {
					vo.setAvgWatchSum(0D);
				} else {
					double duration = playDurationSum / 1000 / watchSum;
					String str = String.format("%.2f", duration);
					vo.setAvgWatchSum(Double.valueOf(str));
				}
			}
			voList.add(vo);
		}
		return voList;
	}

}
